<?php

namespace common\jobs;

use Yii;
use yii\console\Exception;
use common\helpers\Url;
use common\helpers\ArrayHelper;
use youmbe\sports\NanoSports;
use common\models\ofc\OfcTeam;
use common\models\ofc\OfcPlayer;
use common\models\ofc\OfcClub;
use common\models\ofc\OfcVenue;
use common\models\ofc\OfcTeamCoach;

/**
 * 评论之后执行队列任务
 * Class AfterMatchJob.
 */
class AfterTeamJob extends BaseRetryableJob {

    /**
     * @var OfcTeam|null
     */
    public $model = null;

    /**
     * @param \yii\queue\Queue $queue which pushed and is handling the job
     */
    public function execute($queue) {
        if (!$this->model) {
            throw new Exception('AfterTeamJob model is null...');
        }
        $model = $this->model;

        $sports = new NanoSports();
        $data = $sports->team($model->channel_id);
        if (empty($data)) {
            throw new Exception('NanoSports team is null...');
        }

        $venueData = ArrayHelper::getValue($data, 'venue');
        if ($venueData) {
            $venue = OfcVenue::findOrNew([
                'channel_id' => ArrayHelper::getValue($venueData, 'id'),
            ]);
            $venue->name = ArrayHelper::getValue($venueData, 'name_zh');
            $venue->capacity = ArrayHelper::getValue($venueData, 'capacity');
            if ($venue->save()) {
                $model->venue_id = $venue->id;
            }
        }

        $managerData = ArrayHelper::getValue($data, 'manager');
        if ($managerData) {
            $manager = OfcTeamCoach::findOrNew([
                'team_id' => $model->id,
                'channel_id' => ArrayHelper::getValue($managerData, 'id'),
            ]);
            $manager->name = ArrayHelper::getValue($managerData, 'name_zh');
            $manager->name_en = ArrayHelper::getValue($managerData, 'name_en');
            $manager->avatar = Url::attach(ArrayHelper::getValue($managerData, 'logo'), 'https://cdn.leisu.com/avatar/');
            $manager->save();
        }

        $club = $model->club;
        if (!$club) {
            $club = new OfcClub();
            $club->attributes = [
                'name' => $model->name,
                'name_en' => $model->name_en,
                'logo' => $model->logo,
            ];
        }
        $club->venue_id = intval($model->venue_id);
        if ($club->save()) {
            $model->club_id = $club->id;
        }

        $players = ArrayHelper::getValue($data, 'players');
        if (!empty($players)) {
            foreach ($players as $item) {
                $player = OfcPlayer::findOrNew([
                    'channel_id' => ArrayHelper::getValue($item, 'player.id'),
                ]);
                $birthday = ArrayHelper::getValue($item, 'player.birthday');
                $player->attributes = [
                    'name' => ArrayHelper::getValue($item, 'player.name_zh'),
                    'name_en' => ArrayHelper::getValue($item, 'player.name_en', ''),
                    'avatar' => Url::attach(ArrayHelper::getValue($item, 'player.logo'), 'https://cdn.leisu.com/avatar/'),
                    'weight' => ArrayHelper::getValue($item, 'player.weight'),
                    'height' => ArrayHelper::getValue($item, 'player.height'),
                    'nationality' => ArrayHelper::getValue($item, 'player.nationality'),
                    'birthday' => $birthday ? date('Y-m-d', $birthday) : null,
                    'position' => ArrayHelper::getValue($item, 'position'),
                    'shirt_number' => ArrayHelper::getValue($item, 'shirt_number'),
                    'club_id' => $club->id,
                ];
                if (!$player->save()) {
                    Yii::error($player->errors, __METHOD__);
                }
            }
        }
        $model->off($model::EVENT_AFTER_UPDATE, [$model, 'updateTeam']);
        $model->off($model::EVENT_AFTER_INSERT, [$model, 'updateTeam']);

        $model->save();
    }

}
